<?php
	error_reporting(E_ALL || ~E_DEPRECATED || ~E_STRICT || ~E_NOTICE || ~E_WARNING || ~E_CORE_WARNING);
	$myconn=@mysql_connect("localhost","root","root");
    if(!$myconn)
    {
        $response = base64_encode("{\"res\":\"error\"}");
        echo $response;
		die('Could not connect: '.mysql_error());
	    $string = "Could not connect: ".mysql_error();
        file_put_contents("test.log", $string, FILE_APPEND);		
		return;
    }
//	file_put_contents("test.log","\n 02_sendto client message:".$msg.'message len:'.strlen($msg)."\n", FILE_APPEND);
	$db_selected=mysql_select_db("dwaccessDB",$myconn);
	if(!$db_selected)
    {
        mysql_close($myconn);
		$response = base64_encode("{\"res\":\"error\"}");
        echo $response;
		die("Can't use table : ".mysql_error());
	    $string = "Can't use table : ".mysql_error();
        file_put_contents("test.log", $string, FILE_APPEND);	   		
		return;
    }
	$strSql="SELECT controlerNum FROM gatesetting WHERE ((select unix_timestamp(now()))-(select unix_timestamp(lasttime)))>120;";
	$result=mysql_query($strSql,$myconn);
	while($dbResult = mysql_fetch_array($result))
	{
	    $strSql="UPDATE gatesetting SET stat= 'offline'  WHERE controlerNum ='".$dbResult[0]."';";
		$res=mysql_query($strSql,$myconn);
	}
	
	if(!extension_loaded('sockets'))
	{
		@mysql_free_result($result); 	
		mysql_close($myconn);
		$response = base64_encode("{\"res\":\"error\"}");
        echo $response;		
		return;
	}
	
	$Maddress = "239.255.255.250"; //MULTICAST_ADDRESS
    $port = 3702;
//	file_put_contents("test.log","\n 01_sendto client message:".$msg.'message len:'.strlen($msg)."\n", FILE_APPEND);
    
	$strSql="SELECT serverIP,serverPORT FROM serversetting;";
	$result=mysql_query($strSql,$myconn);
	while($dbResult = mysql_fetch_array($result))
	{
	    if($dbResult[0])
        {        	
			if(filter_var($dbResult[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) 			  // it's valid
			{
				$sIP=$dbResult[0];								
			}else                                                                         // it's fail			
			{
				$hostret = gethostbyname($dbResult[0]);
				if(filter_var($hostret, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) 			  
				{
					$sIP=$hostret;
				}else
				{
			    	$sIP="10.10.10.239";
				}
			}
		}else
		{
		    $sIP="10.10.10.239";
		}
		if($dbResult[1])
        {
		    $sPORT=$dbResult[1];
		}else
		{
		    $sPORT = 80;
		}
	}
	@mysql_free_result($result);
//创建端口
    if(($sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) === false) 
	{
	    $response = base64_encode("{\"res\":\"error\"}");
        echo $response;
		$string = "socket_create() failed ,reason:" . socket_strerror(socket_last_error()) . "<br/>\n";
	    file_put_contents("test.log", $string, FILE_APPEND);		
//		return;
    }else
	{	   
//		file_put_contents("test.log","\n recvfrom message null:".$buf.'message len:'.$cc, FILE_APPEND);
		socket_set_option($sock, SOL_SOCKET, SO_SNDTIMEO, array('sec'=>5, 'usec'=>500000));            			        
        //welcome  发送到客户端
//        $IP=@gethostbyname($_ENV['COMPUTERNAME']);
        
        $msg = "{\"devalue\":\"dwserver\","."\"IP\":\"".$sIP."\",\"PORT\":\"".$sPORT."\"}";							
		if(socket_sendto($sock, $msg, strlen($msg),0,$Maddress,$port)===false)
	    {
	        socket_close($sock);
			$response = base64_encode("{\"res\":\"error\"}");
            echo $response;
			$err =  socket_strerror() ;
            file_put_contents("test.log","119err:".$err, FILE_APPEND);						
//			return;
	    }else
	    {
//            file_put_contents("test.log","\n sendto client message:".$msg.'message len:'.strlen($msg)."\n", FILE_APPEND);
	        socket_close($sock);
			$response = base64_encode("{\"res\":\"success\"}");
            echo $response;		    
//		    return;
		}		
	}

//	file_put_contents("test.log","\n 03_sendto client message:".$msg.'message len:'.strlen($msg)."\n", FILE_APPEND);	
   	
    @mysql_free_result($result); 	
	mysql_close($myconn);
?>
